﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.quest.Display {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.filters.*;
    import Display.*;
    import flash.text.*;

    public class ButtonManager {

        protected static var m_registeredTooltips:Array = [];
        protected static var m_registeredButtons:Array = [];
        protected static var m_buttonGroups:Array = [];
        public static var m_effectConfig:XML;

        public static function isCustomButton(_arg1):Boolean{
            var _local3:MovieClip;
            var _local2:Boolean;
            if ((_arg1 is MovieClip)){
                _local3 = (_arg1 as MovieClip);
                if (_local3.framesLoaded == 4){
                    _local2 = true;
                } else {
                    _local2 = false;
                };
            };
            return (_local2);
        }
        public static function buttonize(_arg1, _arg2:String=null, _arg3:String=null, _arg4:Boolean=false):void{
            registerButton(_arg1, _arg2, _arg3, _arg4);
            if ((_arg1 is Sprite)){
                _arg1.buttonMode = true;
                _arg1.useHandCursor = true;
                _arg1.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
                _arg1.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
                _arg1.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
                _arg1.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
                disableMouseOnAllChildText(_arg1);
            };
        }
        public static function enable(_arg1):void{
            if ((_arg1 is Sprite)){
                if (!_arg1.hasEventListener(MouseEvent.MOUSE_OVER)){
                    _arg1.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
                };
                if (!_arg1.hasEventListener(MouseEvent.MOUSE_OUT)){
                    _arg1.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
                };
                if (!_arg1.hasEventListener(MouseEvent.MOUSE_DOWN)){
                    _arg1.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
                };
                if (!_arg1.hasEventListener(MouseEvent.MOUSE_UP)){
                    _arg1.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
                };
            };
            if (!(_arg1 is SimpleButton)){
                _arg1.buttonMode = true;
            };
            _arg1.useHandCursor = true;
            _arg1.filters = [];
        }
        public static function disable(_arg1):void{
            if ((_arg1 is Sprite)){
                if (_arg1.hasEventListener(MouseEvent.MOUSE_OVER)){
                    _arg1.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
                };
                if (_arg1.hasEventListener(MouseEvent.MOUSE_OUT)){
                    _arg1.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
                };
                if (_arg1.hasEventListener(MouseEvent.MOUSE_DOWN)){
                    _arg1.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
                };
                if (_arg1.hasEventListener(MouseEvent.MOUSE_UP)){
                    _arg1.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
                };
            };
            if (!(_arg1 is SimpleButton)){
                _arg1.buttonMode = false;
            };
            _arg1.useHandCursor = false;
            _arg1.filters = [getDisableFilter()];
        }
        public static function getDisableFilter():ColorMatrixFilter{
            var _local1:Number = 0.01;
            var _local2:ColorMatrixFilter = new ColorMatrixFilter();
            var _local3:Array = [1, 0, 0, 0, 0];
            var _local4:Array = [0, 1, 0, 0, 0];
            var _local5:Array = [0, 0, 1, 0, 0];
            var _local6:Array = [0, 0, 0, 1, 0];
            var _local7:Array = [0.3, 0.59, 0.11, 0, 0];
            var _local8:Array = new Array();
            _local8 = _local8.concat(interpolateArrays(_local7, _local3, _local1));
            _local8 = _local8.concat(interpolateArrays(_local7, _local4, _local1));
            _local8 = _local8.concat(interpolateArrays(_local7, _local5, _local1));
            _local8 = _local8.concat(_local6);
            _local2.matrix = _local8;
            return (_local2);
        }
        private static function interpolateArrays(_arg1:Array, _arg2:Array, _arg3:Number):Object{
            var _local4:Array = ((_arg1.length)>=_arg2.length) ? _arg1.slice() : _arg2.slice();
            var _local5:int = _local4.length;
            while (_local5--) {
                _local4[_local5] = (_arg1[_local5] + ((_arg2[_local5] - _arg1[_local5]) * _arg3));
            };
            return (_local4);
        }
        protected static function disableMouseOnAllChildText(_arg1:Sprite):void{
            var _local2:MovieClip;
            var _local3:int;
            var _local4:*;
            if ((_arg1 is MovieClip)){
                _local2 = (_arg1 as MovieClip);
                _local3 = 0;
                while (_local3 < _local2.numChildren) {
                    _local4 = _local2.getChildAt(_local3);
                    if ((_local4 is TextField)){
                        _local4.mouseEnabled = false;
                    };
                    _local3++;
                };
            };
        }
        public static function forceSelected(_arg1):void{
            setSelected(_arg1);
        }
        protected static function setSelected(_arg1):void{
            var _local3:*;
            var _local4:MovieClip;
            var _local2:Object = findButtonData(_arg1);
            if (!_local2){
                return;
            };
            if (_local2.groupName){
                for each (_local3 in m_buttonGroups[_local2.groupName]) {
                    _local2 = findButtonData(_local3);
                    if (_local3 == _arg1){
                        if (isCustomButton(_local3)){
                            _local4 = _local3;
                            _local2.selected = true;
                            _local4.gotoAndStop(3);
                        } else {
                            applyFilter(_local3);
                        };
                    } else {
                        if (isCustomButton(_local3)){
                            _local4 = _local3;
                            _local2.selected = false;
                            _local4.gotoAndStop(1);
                        } else {
                            removeFilter(_local3);
                        };
                    };
                };
            } else {
                _local2.selected = !(_local2.selected);
            };
        }
        protected static function findButtonData(_arg1):Object{
            var _local2:Object;
            var _local3:*;
            for each (_local3 in m_registeredButtons) {
                if (_local3.image == _arg1){
                    _local2 = _local3;
                };
            };
            return (_local2);
        }
        protected static function onMouseOver(_arg1:MouseEvent):void{
            var _local4:MovieClip;
            var _local2:Object = findButtonData(_arg1.currentTarget);
            var _local3:* = _local2.image;
            if (isCustomButton(_local3)){
                _local4 = (_local3 as MovieClip);
                _local4.gotoAndStop(3);
            } else {
                applyFilter(_local3);
            };
        }
        protected static function onMouseOut(_arg1:MouseEvent):void{
            var _local4:MovieClip;
            var _local2:Object = findButtonData(_arg1.currentTarget);
            var _local3:* = _local2.image;
            if (isCustomButton(_local3)){
                _local4 = (_local3 as MovieClip);
                if (_local2.selected){
                    _local4.gotoAndStop(3);
                } else {
                    _local4.gotoAndStop(1);
                };
            } else {
                if ((_local3 is Sprite)){
                    removeFilter(_local3);
                };
            };
        }
        protected static function onMouseDown(_arg1:MouseEvent):void{
            var _local4:MovieClip;
            var _local2:Object = findButtonData(_arg1.currentTarget);
            var _local3:* = _local2.image;
            if (isCustomButton(_local3)){
                _local4 = (_local3 as MovieClip);
                _local4.gotoAndPlay(2);
            } else {
                applyFilter(_local3);
            };
        }
        protected static function onMouseUp(_arg1:MouseEvent):void{
            var _local4:MovieClip;
            var _local2:Object = findButtonData(_arg1.currentTarget);
            var _local3:* = _local2.image;
            if (isCustomButton(_local3)){
                _local4 = (_local3 as MovieClip);
                _local4.gotoAndPlay(3);
            } else {
                applyFilter(_local3);
            };
            setSelected(_local3);
        }
        protected static function removeFilter(_arg1):void{
            _arg1.filters = [];
        }
        protected static function applyFilter(_arg1):void{
            var buttonData:* = null;
            var effectName:* = null;
            var effectsConfig:* = null;
            var glowFilterConfig:* = null;
            var obj:* = _arg1;
            var blurStrength:* = Math.pow((obj.width * obj.height), 0.15);
            var blurX:* = blurStrength;
            var blurY:* = blurStrength;
            var strength:* = 500;
            var color:* = 0xFFFFFF;
            var alpha:* = 1;
            for each (buttonData in m_registeredButtons) {
                if (buttonData.image == obj){
                    effectName = buttonData.effectName;
                };
            };
            if (effectName){
                effectsConfig = Global.gameSettings.m_effectsConfigXml;
                glowFilterConfig = effectsConfig.glowFilter.(@name == effectName)[0];
                color = parseInt(String(glowFilterConfig.@color));
                alpha = glowFilterConfig.@alpha;
                blurX = glowFilterConfig.@blurX;
                blurY = glowFilterConfig.@blurY;
                strength = glowFilterConfig.@strength;
            };
            var gf:* = new GlowFilter(color, alpha, blurX, blurY, strength);
            obj.filters = [gf];
        }
        protected static function registerButton(_arg1, _arg2:String, _arg3:String=null, _arg4:Boolean=false):void{
            var _local7:*;
            var _local8:MovieClip;
            var _local5:Object = new Object();
            _local5.image = _arg1;
            _local5.groupName = _arg2;
            _local5.effectName = _arg3;
            m_registeredButtons.push(_local5);
            _local5.selected = _arg4;
            var _local6:Array = m_buttonGroups[_arg2];
            if (!_local6){
                _local6 = (m_buttonGroups[_arg2] = []);
            };
            _local6.push(_arg1);
            if (((_arg4) && (_local5.groupName))){
                for each (_local7 in m_buttonGroups[_local5.groupName]) {
                    _local5 = findButtonData(_local7);
                    if (_local7 == _arg1){
                        if (isCustomButton(_local7)){
                            _local8 = _local7;
                            _local8.gotoAndStop(3);
                        };
                    };
                };
            };
        }
        public static function applyToolTip(_arg1:Sprite, _arg2:String):void{
            registerTooltip(_arg1, _arg2);
        }
        protected static function findRegisteredTooltipBySprite(_arg1:Sprite):Object{
            var _local2:Object;
            for each (_local2 in m_registeredTooltips) {
                if (_local2.image == _arg1){
                    return (_local2);
                };
            };
            return (null);
        }
        public static function removeToolTip(_arg1:Sprite):void{
            var _local2:Object;
            for each (_local2 in m_registeredTooltips) {
                if (_local2.image == _arg1){
                    m_registeredTooltips.splice(m_registeredTooltips.indexOf(_local2), 1);
                };
            };
        }
        protected static function registerTooltip(_arg1:Sprite, _arg2:String):void{
            var _local5:StrokeTextField;
            var _local3:Object = findRegisteredTooltipBySprite(_arg1);
            if (!_local3){
                _local3 = new Object();
                m_registeredTooltips.push(_local3);
                _arg1.addEventListener(MouseEvent.MOUSE_MOVE, onMouseOverTooltip);
                _arg1.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutTooltip);
            };
            var _local4:Sprite = new Sprite();
            var _local6:Number = 2;
            _local5 = new StrokeTextField(0x202020, 0.8);
            _local5.multiline = true;
            _local5.wordWrap = true;
            _local5.width = 200;
            _local5.autoSize = TextFieldAutoSize.LEFT;
            _local5.cacheAsBitmap = true;
            _local5.defaultTextFormat = new TextFormat("Arial", 14, 0xFFFFFF, true);
            _local5.toolTipText = _arg2;
            var _local7:BitmapData = new BitmapData((_local5.textWidth + (_local6 * 2)), (_local5.textHeight + (_local6 * 2)), true, 0);
            _local7.draw(_local5);
            var _local8:Bitmap = new Bitmap(_local7);
            _local4.addChild(_local8);
            _local4.graphics.beginFill(0, 0.4);
            _local4.graphics.drawRoundRect(0, 0, (_local5.textWidth + (_local6 * 2)), (_local5.textHeight + (_local6 * 2)), 8, 8);
            _local4.graphics.endFill();
            _local4.visible = false;
            _local4.mouseEnabled = false;
            _local3.image = _arg1;
            _local3.tooltip = _local4;
            Global.stage.addChild(_local3.tooltip);
        }
        protected static function onMouseOverTooltip(_arg1:MouseEvent):void{
            var _local3:Object;
            var _local4:Sprite;
            var _local2:Sprite = (_arg1.currentTarget as Sprite);
            for each (_local3 in m_registeredTooltips) {
                if (_local3.image == _local2){
                    _local4 = _local3.tooltip;
                    _local4.visible = true;
                    _local4.mouseEnabled = true;
                    updateTooltipPosition(_local4);
                };
            };
        }
        protected static function onMouseOutTooltip(_arg1:MouseEvent):void{
            var _local3:Object;
            var _local4:Sprite;
            var _local2:Sprite = (_arg1.currentTarget as Sprite);
            for each (_local3 in m_registeredTooltips) {
                if (_local3.image == _local2){
                    _local4 = _local3.tooltip;
                    _local4.visible = false;
                    _local4.mouseEnabled = false;
                };
            };
        }
        protected static function updateTooltipPosition(_arg1:Sprite):void{
            var _local2:Point = new Point((Global.stage.mouseX + 10), (Global.stage.mouseY + 10));
            if ((Global.stage.stageWidth - _arg1.width) < _local2.x){
                _local2.x = ((Global.stage.mouseX - _arg1.width) - 10);
            };
            if ((Global.stage.stageHeight - _arg1.height) < _local2.y){
                _local2.y = ((Global.stage.mouseY - _arg1.height) - 10);
            };
            _arg1.x = _local2.x;
            _arg1.y = _local2.y;
        }

    }
}//package Modules.quest.Display 
